<template>
  <div class="result">
    <van-nav-bar
      :title="key + '的搜索结果'"
      left-arrow
      @click-left="$router.push('/home/search')"
    />

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
        @click="toDetail(item.art_id)"
      />
    </van-list>
  </div>
</template>

<script>
import { getResultAPI } from "@/api";
export default {
  data() {
    return {
      key: this.$route.query.key,
      loading: false,
      finished: false,
      list: [],
      page: 1,
      per_page: 2,
      total: 1,
    };
  },

  methods: {
    toDetail(art_id) {
      this.$router.push(`/home/detail?id=${art_id}`);
    },

    async onLoad() {
      const res = await getResultAPI({
        page: this.page,
        per_page: this.per_page,
        q: this.key,
      });

      this.list = [...this.list, ...res.data.data.results];
      this.loading = false;
      this.total = res.data.data.total_count;
      this.finished = false;
      if (this.list.length >= this.total) {
        this.finished = true;
      }
    },
  },
};
</script>

<style lang="less">
.result {
  .van-nav-bar__title.van-ellipsis {
    color: #fff;
  }
  i.van-icon.van-icon-arrow-left.van-nav-bar__arrow {
    color: #fff;
  }
}
</style>